<?php
class Users extends Model{
	
    function getData(){
		$sql ="SELECT *,u.id FROM users u ";
		$sql.="LEFT JOIN user_group g ON g.id=u.group_id ";
		$sql.="LEFT JOIN user_position p ON p.id=u.pos_id ";
        $sql.="WHERE u.username = '".$this->session->userdata('user_id')."' LIMIT 1";
		$query=$this->db->query($sql);
		return $query->row_array();
	}
    
    function getCekPassword(){
        $sql ="SELECT * FROM users u ";
		$sql.="WHERE password='".SHA1($this->input->post('oldpassword'))."' AND username = '".$this->session->userdata('user_id')."' LIMIT 1";
        $query=$this->db->query($sql);
		return $query->row_array();
    }
    
    function getPos(){
		$sql ="SELECT * FROM user_position p ";
        $sql .="WHERE p.enabled =1 ";
		$query=$this->db->query($sql);
		return $query->result_array();
	}
    
    function getGroup(){
		$sql ="SELECT * FROM user_group ";
		$query=$this->db->query($sql);
		return $query->result_array();
	}
    
    function getUpdate(){
        $value = array  (
                            'group_id'  =>  $this->input->post('group'),
                            'pos_id'    =>  $this->input->post('pos'),
                            'name'      =>  $this->input->post('name'),
                            //'username'  =>  $this->input->post('user_call'),
                            'birthday'  =>  $this->input->post('birthday'),
                            'status'    =>  $this->input->post('status'),
                            'gender'    =>  $this->input->post('gender'),
                            'address'   =>  $this->input->post('address'),
                            'tlp'       =>  $this->input->post('tlp'),
                            'email'     =>  $this->input->post('email'),
                            'city'      =>  $this->input->post('city'),
                            'zip_code'  =>  $this->input->post('zipcode'),
                            'hobby'     =>  $this->input->post('hobby'),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->where('username',$this->session->userdata('user_id'));                 
        $this->db->update('users',$value);                
    }
    
    function getPassword(){
        $value = array  (
                            'password'  =>  SHA1($this->input->post('password')),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->where('username',$this->session->userdata('user_id'));                 
        $this->db->update('users',$value);                               
    }
    
}